#include <iostream>

using std::cout;
using std::endl;

void adjustHeap(int *arr, int k)
{
    int i = 0, val = arr[i];
    while(i <= k / 2 - 1){
        int child = i * 2 + 1;
        if(child + 1 < k && arr[child+1] < arr[child]){
            child = child + 1;
        }
        if(arr[child] < arr[i]){
            arr[i] = arr[child];
            i = child;
        }else{
            break;
        }
    }
    arr[i] = val;
}

void test1(int *arr, int k, int len)
{
    adjustHeap(arr, 3);
    for(int i{k}; i < len; i++){
        if(arr[i] > arr[0]){
            arr[0] = arr[i];
            adjustHeap(arr, k);
        }
    }

    for(int i{}; i < len; i++){
        cout << arr[i] << " ";
    }

    cout << endl;
}

int main(int args, char *argv[])
{
    int arr[] = {5, 3, 1, 6, 8, 2, 8, -1, 100};
    int len = sizeof(arr) / sizeof(int);
    test1(arr, 3, len);
    return 0;
}